part of 'fragment.dart';

// ignore: camel_case_types
class _PaymentMethodFragmentView_600_900 extends StatelessWidget {
  const _PaymentMethodFragmentView_600_900({
    required _PaymentMethodFragmentWloc wloc,
  }) : _wloc = wloc;

  final _PaymentMethodFragmentWloc _wloc;

  Widget _buildContent({
    required _PaymentMethodFragmentWlocState state,
    required BuildContext context,
  }) {
    if (state is _InitialPaymentMethodFragmentWlocState) {
      return _buildInitialStateContent(
        state: state,
        context: context,
      );
    }

    if (state is _LoadingPaymentMethodFragmentWlocState) {
      return _buildLoadingStateContent(
        state: state,
        context: context,
      );
    }

    if (state is _LoadedPaymentMethodFragmentWlocState) {
      return _buildLoadedStateContent(
        state: state,
        context: context,
      );
    }

    throw UnimplementedError();
  }

  Widget _buildInitialStateContent({
    required _InitialPaymentMethodFragmentWlocState state,
    required BuildContext context,
  }) {
    return const SizedBox();
  }

  Widget _buildLoadingStateContent({
    required _LoadingPaymentMethodFragmentWlocState state,
    required BuildContext context,
  }) {
    return const DelayedWidget(
      delay: Duration(milliseconds: 500),
      child: Center(
        child: AppLoader(),
      ),
    );
  }

  Widget _buildLoadedStateContent({
    required _LoadedPaymentMethodFragmentWlocState state,
    required BuildContext context,
  }) {
    final widgets = <Widget>[
      const SizedBox(
        height: 48,
      ),
      Row(
        children: [
          Flexible(
            child: _buildTitle(
              context: context,
            ),
          ),
          if (state.webSavedPaymentMethods.isNotEmpty) ...[
            const SizedBox(
              width: 16,
            ),
            if (state.editingMode)
              _EditingCompletionButton(
                size: 36,
                onPressed: _wloc.onEditingCompletionButtonPressed,
              )
            else
              _EditingButton(
                size: 36,
                onPressed: _wloc.onEditingButtonPressed,
              ),
          ],
        ],
      ),
    ];

    if (state.webSavedPaymentMethods.isNotEmpty) {
      widgets.addAll([
        const SizedBox(
          height: 36,
        ),
        const _Divider(),
        const SizedBox(
          height: 36,
        ),
      ]);

      for (var i = 0; i < state.webSavedPaymentMethods.length; i++) {
        if (i > 0) {
          widgets.add(
            const SizedBox(
              height: 20,
            ),
          );
        }

        widgets.add(_buildPaymentMethodTile(
          paymentMethod: state.webSavedPaymentMethods[i],
          state: state,
          context: context,
        ));
      }
    }

    widgets.add(const SizedBox(
      height: 48,
    ));

    return Row(
      children: [
        const Spacer(),
        Expanded(
          flex: 10,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: widgets,
          ),
        ),
        const Spacer(),
      ],
    );
  }

  Widget _buildTitle({
    required BuildContext context,
  }) {
    return buildMarkdown(
      markdown: PaymentMethodFragmentTranslation.of(context).titleMarkdown,
      textStyle: PaymentMethodFragmentTheme.of(context).textTheme.title,
      textAlign: TextAlign.start,
      context: context,
      markedTextStyle: TextStyle(
        color: CoreTheme.of(context).primaryColor,
      ),
    );
  }

  Widget _buildPaymentMethodTile({
    required WebPaymentMethod paymentMethod,
    required _LoadedPaymentMethodFragmentWlocState state,
    required BuildContext context,
  }) {
    final iconAssetPath = _wloc.getPaymentMethodIconAssetPath(
      paymentMethod: paymentMethod,
    );

    final title = _wloc.getPaymentMethodTitle(
      paymentMethod: paymentMethod,
    );

    final expirationTimeLabel = _wloc.getPaymentMethodExpirationTimeLabel(
      paymentMethod: paymentMethod,
    );

    final failureMessage = _wloc.getPaymentMethodFailureMessage(
      paymentMethod: paymentMethod,
    );

    final double opacity;

    if (paymentMethod.failure == null) {
      opacity = 1.0;
    } else {
      opacity = _kFailedPaymentMethodTileOpacity;
    }

    return Row(
      children: [
        Opacity(
          opacity: opacity,
          child: Image.asset(
            iconAssetPath,
            width: 45,
            height: 45,
            filterQuality: FilterQuality.high,
          ),
        ),
        const SizedBox(
          width: 16,
        ),
        Opacity(
          opacity: opacity,
          child: SizedBox(
            width: 200,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: PaymentMethodFragmentTheme.of(context).textTheme.tileTitle,
                ),
                const SizedBox(
                  height: 4,
                ),
                Text(
                  expirationTimeLabel,
                  style: PaymentMethodFragmentTheme.of(context).textTheme.tileExpirationTimeLabel,
                ),
                if (failureMessage != null) ...[
                  const SizedBox(
                    height: 4,
                  ),
                  Text(
                    failureMessage,
                    style: PaymentMethodFragmentTheme.of(context).textTheme.tileFailureMessage,
                  ),
                ],
              ],
            ),
          ),
        ),
        if (state.editingMode) ...[
          const SizedBox(
            width: 16,
          ),
          _PaymentMethodRemovalButton(
            size: 30,
            onPressed: () {
              _wloc.onPaymentMethodRemovalButtonPressed(
                paymentMethod: paymentMethod,
              );
            },
          )
        ],
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      color: CoreTheme.of(context).backgroundColor,
      child: WlocBuilder<_PaymentMethodFragmentWloc, _PaymentMethodFragmentWlocState>(
        wloc: _wloc,
        builder: (context, state) {
          return _buildContent(
            state: state,
            context: context,
          );
        },
      ),
    );
  }
}
